package freespiritsocial.io.user.ui.fragment.account

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.afollestad.date.dayOfMonth
import com.afollestad.date.month
import com.afollestad.date.year
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import freespiritsocial.io.user.R
import freespiritsocial.io.user.base.BaseFragment
import freespiritsocial.io.user.databinding.FrgSignUpSup2Binding
import freespiritsocial.io.user.ext.getTimeFormat
import freespiritsocial.io.user.handle.BackHandle
import freespiritsocial.io.user.ui.activity.MainAct
import freespiritsocial.io.user.util.CacheUtil
import freespiritsocial.io.user.viewmodel.SignUpModel
import freespiritsocial.io.user.weight.PictureDialog
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import java.util.*

class SignSup2UpFrg: BaseFragment<SignUpModel,FrgSignUpSup2Binding>() {

    companion object {
        const val key_third_name = "ThirdName"
    }

    val pictureDialog by lazy(LazyThreadSafetyMode.NONE) {
        PictureDialog(this,isShowAll = false) {
            if (it.isNotEmpty())
                mViewModel.userAvatar.set( if (it[0].isCompressed) it[0].compressPath else it[0].realPath)
        }
    }

    private val minType = booleanArrayOf(true,true,true,false,false,false)
    private val durationPicker: TimePickerView by lazy(LazyThreadSafetyMode.NONE) {
        TimePickerBuilder(requireContext(),
            OnTimeSelectListener { date,_ ->
                mViewModel.dob.set(date.getTimeFormat())
            })
            .setLayoutRes(R.layout.layout_time_picker) {
                it.findViewById<TextView>(R.id.btnCancel).setOnClickListener {
                    durationPicker.dismiss()
                }
                it.findViewById<TextView>(R.id.btnSubmit).setOnClickListener {
                    durationPicker.returnData()
                    durationPicker.dismiss()
                }
            }
            .setRangDate(Calendar.getInstance().apply {
                year = 1900
                month = 1
                dayOfMonth = 1
            },Calendar.getInstance())
            .setDate(Calendar.getInstance())
            .setType(minType)
            .setLabel("","","","","","")
            .setContentTextSize(20)
            .setLineSpacingMultiplier(1.8f)
            .setDecorView(
                activity?.window?.decorView
                    ?.findViewById(android.R.id.content) as ViewGroup
            )
            .isDialog(false)
            .setBgColor(Color.WHITE)
            .build()
    }

    override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
        //设置view model作用域
        viewModelOwner = (activity as MainAct).accountViewModelOwner
        super.onViewCreated(view,savedInstanceState)
    }

    override fun layoutId(): Int = R.layout.frg_sign_up_sup2

    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.isThirdSignUp.set(arguments?.getString(key_third_name) != null)
        mViewModel.thirdPartyId.set(arguments?.getString(key_third_name,""))
        mViewModel.thirdPartyType.set(arguments?.getInt("thirdPartyType",0))
        mDatabind.viewModel = mViewModel
        mDatabind.click = Click()
        mDatabind.tvCountry.setOnCountryChangeListener {
            mViewModel.country.set(mDatabind.tvCountry.selectedCountryEnglishName)
        }
    }

    override fun onDestroyView() {
        if (mViewModel.isThirdSignUp.get()) {
            viewModelOwner?.viewModelStore?.clear()
        }
        super.onDestroyView()
    }

    inner class Click: BackHandle {
        fun signUp() {
            mViewModel.register {
                CacheUtil.token = it
                nav().navigateAction(R.id.action_signSup2UpFrg_to_discoverPeopleFrg)
            }
        }

        fun removeAvatar() {
            pictureDialog.removeData(mViewModel.userAvatar.get())
            mViewModel.userAvatar.set("")
        }

        fun selectPic() {
            pictureDialog.show(childFragmentManager,"picture")
        }

        fun selectDob() {
            durationPicker.show()
        }

        override fun goBack() {
            nav().navigateUp()
        }
    }
}